Results 1 to 7 of 7

Thread: Help with arrays again...

  1. #1
    Purple Developer Purple Elephant1au's Avatar
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    1,259

    Default Help with arrays again...

    Hi

    Yea ive run into a problem again...

    What is the right way to check if something is already in an array and if it isnt , add it , and if it is , dont

    My try was something like
    Code:
    test local.print:
    
    level.test[level.num] = local.print
    
    for (local.i = 1; local.i <= level.test.size; local.i++)
        { 
            if(local.print != level.test[local.i])
            {
            println(local.print)
            level.num++      
            }
        
        }    
     
    end
    with things like
    waitthread global/test::test "Print1"
    waitthread global/test::test "Print2"
    waitthread global/test::test "Print1"
    and level.num = 1 to start with

    That was my guess at first but its nowhere close lol it prints it each time even if i have the same local.print already in the array

    So whats the right way to do it lol my mind just isnt in the game tonight

    Thanks , im sure you guys can just quickly tell me where i went wrong lol
    Last edited by Purple Elephant1au; June 1st, 2013 at 05:46 AM.

    Purple's Playground
    OBJ :
    103.29.85.127:12203
    xfire: purpleelephant1au
    email: purpleelephant1au@gmail.com
    skydrive: PurpleElephantSkydrive




  2. #2
    Developer Sor's Avatar
    Join Date
    Aug 2010
    Location
    The Medieval City of Bruges
    Posts
    747

    Default

    In a nutshell:
    Code:
    test local.toadd:
    for (local.i = 1; local.i <= level.test.size; local.i++) {
         if (level.test[local.i] == local.toadd) { //check if already added
              end; // don't need to add twice
         }
    }
    level.test[level.test.size + 1] = local.toadd //nothing found, add away
    end
    Also, don't use global indices.

    If you're manipulating arrays, you might want to take a look at the 'Array' class in the framework's BETA version.
    Morpheus Script (MoH) => You try to shoot yourself in the foot only to discover that MorpheusScript already shot your foot for you.

  3. #3
    Purple Developer Purple Elephant1au's Avatar
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    1,259

    Default

    Thanks, its kinda working , but only working if i add quotes to the waitthread global/test::test "Print1"
    tried without them and nothing lol

    Any reason why the array.size starts a -1 , then goes to 1

    I added a printIn just before the level.test[level.test.size +1] = local.print

    ~~~~ Size = -1
    ~~~~ Size = 1
    ~~~~ Size = 2
    ~~~~ Size = 3

    And ill have a loot at the framework soon :P

    Purple's Playground
    OBJ :
    103.29.85.127:12203
    xfire: purpleelephant1au
    email: purpleelephant1au@gmail.com
    skydrive: PurpleElephantSkydrive




  4. #4
    Developer Sor's Avatar
    Join Date
    Aug 2010
    Location
    The Medieval City of Bruges
    Posts
    747

    Default

    If a variable is uninitialized, its size is -1. Or rather, the size of NIL is -1.
    Code:
    println local.arr.size
    If an array is initialized but hasn't been assigned a single value, then its size is 0:
    Code:
    local.arr[0] = NIL
    println local.arr.size
    Here we defined an array but there is nothing with a size in that array so its size remains 0.

    Each variable or datatype has a size. For instance, NULL has size 1 as it refers to the 'NULL-entity'.
    A vector, surprisingly, also has size 1 despite consisting of 3 values that can be accessed like an array.

    In essence, you initialize the array with the line above (doesn't actually matter what index NIL is put in),
    and then you add variables to it either with its size (for a 0-based array) or with its size + 1 (for 1-based arrays).
    Last edited by Sor; June 2nd, 2013 at 01:48 PM.
    Morpheus Script (MoH) => You try to shoot yourself in the foot only to discover that MorpheusScript already shot your foot for you.

  5. #5
    Developer Sor's Avatar
    Join Date
    Aug 2010
    Location
    The Medieval City of Bruges
    Posts
    747

    Default

    Quote Originally Posted by Purple Elephant1au View Post
    And ill have a loot at the framework soon :P
    You might like it. The array class has a 'Serialize()' function that turns an array into a string that can be saved in a cvar,
    and later you can just retrieve the string and 'Deserialize()' it back into an array. Data as in both their value as their type is
    kept in-tact. An int will stay an int, a float will stay a float... but all entities will have become NULL (for obvious reasons).

    Don't know if that is the aim here like with your weapon swap mod, but if it is, I got it covered
    Morpheus Script (MoH) => You try to shoot yourself in the foot only to discover that MorpheusScript already shot your foot for you.

  6. #6
    Purple Developer Purple Elephant1au's Avatar
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    1,259

    Default

    Yea i looked over the framework and WOW , cant wait to start using it :P , i could nearly rewrite alot of my mods to incorporate this :P
    But with me i think ill need to wait for some documentation on how to use it lol

    Purple's Playground
    OBJ :
    103.29.85.127:12203
    xfire: purpleelephant1au
    email: purpleelephant1au@gmail.com
    skydrive: PurpleElephantSkydrive




  7. #7
    Developer Sor's Avatar
    Join Date
    Aug 2010
    Location
    The Medieval City of Bruges
    Posts
    747

    Default

    Did you check the source files? Each public, finished function is preceded by a comment with what it does in words and its return values explained.

    The header of the comment will already explain much:
    Code:
    void Sort(<array> array, [<bool> descending], [<array:string> compareThread])
    The return value is 'void', meaning it will not return anything (i.e. NIL). Then we have the name of the function followed by the arguments to pass.
    Between <> brackets are the datatypes of those values. Type 'array' simply means it doesn't matter what type of array. Type 'array:string' means it
    doesn't matter what array but it must only contain string types.
    Type 'bool' just means that one either needs to pass 0 (for false) or 1 (for true) but actually it doesn't matter as all values are evaluated as false
    (being 0, 0.0, "", NIL, NULL) or true (all others). But 0/1 look cleaner, though.

    Arguments between rectangular brackets are optional. This means that if you do not specify them, they default to whatever is mentioned in the comment.

    EDIT: If a function does fail for you, use level.DEBUG by setting it to 1. It will likely cause an error message to be printed in the console, unless you managed
    to undermine the function's working in a way I did not anticipate.. which is possible, because I tried not to focus too much on error-checking.
    Hence why error-checking rarely occur if level.DEBUG is false; it unnecessarily slows down the procedure. I figured the checks for common stuff should be
    done at design-time, so the mod can be faster once in run-time by skipping checks for stuff that was tested to not fail.
    Last edited by Sor; June 2nd, 2013 at 01:50 PM.
    Morpheus Script (MoH) => You try to shoot yourself in the foot only to discover that MorpheusScript already shot your foot for you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •